package com.ygxsk.carhome.ui.evaluate;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.StringUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.AgreenMentFileAddSubmit;
import com.ygxsk.carhome.rxbus.RxBusCalendarTime;
import com.ygxsk.carhome.rxbus.RxBusCalendarTimeForAgreement;
import com.ygxsk.carhome.rxbus.RxBusCalendarTimeForSignatureAdd;

import java.util.HashMap;
import java.util.Map;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-02-26  18:54
 * @Description:自定义一个日历的弹窗内容 用于服务站经理的业务台账
 */
public class CalendarViewCustomPopup extends BottomPopupView implements CalendarView.OnCalendarSelectListener, CalendarView.OnYearChangeListener {//
//public class CalendarViewCustomPopup extends FullScreenPopupView implements CalendarView.OnCalendarSelectListener, CalendarView.OnYearChangeListener, View.OnClickListener {//


    RelativeLayout mRelativeTool;
    private int mYear;
    //月份和当天
    TextView mTextMonthDay;
    //当前的年份
    TextView mTextYear;
    //日历
    TextView mTextLunar;
    //日历的一个布局
    FrameLayout fl_current;
    //日历的一个图片内容
    ImageView ib_calendar;
    //日历的当前天
    TextView mTextCurrentDay;
    //当前整个日历布局
    CalendarView mCalendarView;
//    CalendarLayout mCalendarLayout;

    //是否携带标记；
    private boolean isWithMark;
    private int extra_value;
    //关于携带的id信息，判断是哪个值内容携带过来的
    private int extra_identifierNumber;

    //设置一个 即将被添加的合同；
    private AgreenMentFileAddSubmit agreenMentFileAddSubmit;

    public void setAgreenMentFileAddSubmit(AgreenMentFileAddSubmit _agreenMentFileAddSubmit) {
        this.agreenMentFileAddSubmit = _agreenMentFileAddSubmit;
    }

    public CalendarViewCustomPopup(int _extra_value, @NonNull Context context) {
        super(context);
//        isWithMark = _isWithMark;
        extra_value = _extra_value;
    }

    public CalendarViewCustomPopup(int _extra_value, int _identifierNumber, @NonNull Context context, boolean _isWithMark) {
        super(context);
        isWithMark = _isWithMark;
        extra_value = _extra_value;
        extra_identifierNumber = _identifierNumber;
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.calendarviewcustompop;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        //这里直接填充布局的，
        mTextMonthDay = findViewById(R.id.tv_month_day);
        mTextYear = findViewById(R.id.tv_year);
        mTextLunar = findViewById(R.id.tv_lunar);
        mRelativeTool = findViewById(R.id.rl_tool);
        mCalendarView = findViewById(R.id.calendarView);
        mTextCurrentDay = findViewById(R.id.tv_current_day);

        if (mTextMonthDay != null) {
            mTextMonthDay.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

//                if (!mCalendarLayout.isExpand()) {
//                    mCalendarLayout.expand();
//                    return;
//                }

                    mCalendarView.showYearSelectLayout(mYear);
                    mTextLunar.setVisibility(View.GONE);
                    mTextYear.setVisibility(View.GONE);
                    mTextMonthDay.setText(String.valueOf(mYear));

                }
            });
        }

        if (findViewById(R.id.fl_current) != null) {
            findViewById(R.id.fl_current).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCalendarView.scrollToCurrent();
                }
            });
        }

        //mCalendarLayout = findViewById(R.id.calendarLayout);
        if (mCalendarView != null) {
            mCalendarView.setOnYearChangeListener(this);
        }

        if (mCalendarView != null) {
            mCalendarView.setOnCalendarSelectListener(this);
        }


        mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
        mYear = mCalendarView.getCurYear();
        mTextMonthDay.setText(mCalendarView.getCurMonth() + StringUtils.getString(R.string.monthinpopu) + mCalendarView.getCurDay() + StringUtils.getString(R.string.dayinpopu));
        mTextLunar.setText(StringUtils.getString(R.string.todayinpopu));

        mTextCurrentDay.setText(String.valueOf(mCalendarView.getCurDay()));


        //如果需要标记，那么就标记下；
        if (isWithMark) {

//        int year = mCalendarView.getCurYear();
//        int month = mCalendarView.getCurMonth();
            //一个指定的拼接的当天
            //直接从MMKV之中取出；
            int mmkv_year = MMKV.defaultMMKV().decodeInt(ConstantSign.MMKV_KEY_YEAR);
            int mmkv_month = MMKV.defaultMMKV().decodeInt(ConstantSign.MMKV_KEY_MONTH);
            int mmkv_day = MMKV.defaultMMKV().decodeInt(ConstantSign.MMKV_KEY_DAY);


            Map<String, Calendar> map = new HashMap<>();
            map.put(getSchemeCalendar(mmkv_year, mmkv_month, mmkv_day, ColorUtils.getColor(R.color.colorPrimary), getContext().getString(R.string.workbookinputtime)).toString(),//
                    getSchemeCalendar(mmkv_year, mmkv_month, mmkv_day, ColorUtils.getColor(R.color.colorPrimary), getContext().getString(R.string.workbookinputtime)));

            //此方法在巨大的数据量上不影响遍历性能，推荐使用
            mCalendarView.setSchemeDate(map);//标记日期

        }

    }

    @Override
    protected void onShow() {
        super.onShow();

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "界面弹窗 弹出");

    }

    @Override
    protected boolean onBackPressed() {
//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "拦截 弹出弹窗的  返回事件");
        return super.onBackPressed();
    }


    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

        //暂时不做处理

    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .6f);
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {

        if (mTextLunar != null) {
            mTextLunar.setVisibility(View.VISIBLE);
        }

        if (mTextYear != null) {
            mTextYear.setVisibility(View.VISIBLE);
        }

        if (mTextMonthDay != null) {
            mTextMonthDay.setText(calendar.getMonth() + StringUtils.getString(R.string.monthinpopu) + calendar.getDay() + StringUtils.getString(R.string.dayinpopu));
        }

        if (mTextYear != null) {
            mTextYear.setText(String.valueOf(calendar.getYear()));
        }

        if (mTextLunar != null) {
            mTextLunar.setText(calendar.getLunar());
        }

        mYear = calendar.getYear();

        //最好使用 格式化之后：String.format("%02d", i)
//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "选中的时间是-->" + mYear + String.format("%02d", calendar.getMonth()) + String.format("%02d", calendar.getDay()));

        if (extra_value == ExtraValue.RXBUS_EVALUATE_CAR_MANUFACTURE_TO_ADDSUBMIT) {//如果是车辆评估的车辆出厂日期时间

            //如果处理数据，那么处理为;
            RxBusCalendarTime rxBusCalendarTime = new RxBusCalendarTime();
            rxBusCalendarTime.setActionFlag(ExtraValue.RXBUS_EVALUATE_CAR_MANUFACTURE_TO_ADDSUBMIT);
            rxBusCalendarTime.setManufactureDate(calendar.getYear() + "-" + String.format("%02d", calendar.getMonth()) + "-" + String.format("%02d", calendar.getDay()));

            RxBus.getInstance().post(rxBusCalendarTime);

        } else if (extra_value == ExtraValue.RXBUS_EVALUATE_CAR_AUTH_SYSTEM_TO_ADDSUBMIT) {//添加车辆认证时间

            //如果处理数据，那么处理为;
            RxBusCalendarTime rxBusCalendarTime = new RxBusCalendarTime();
            rxBusCalendarTime.setActionFlag(ExtraValue.RXBUS_EVALUATE_CAR_AUTH_SYSTEM_TO_ADDSUBMIT);
            rxBusCalendarTime.setManufactureDate(calendar.getYear() + "-" + String.format("%02d", calendar.getMonth()) + "-" + String.format("%02d", calendar.getDay()));

            RxBus.getInstance().post(rxBusCalendarTime);

        } else if (extra_value == ExtraValue.MODIFY_ARCHIVEFILE_AGREEMENT_TIME_SELECT) {//是从【添加合同】的选择签约时间过来的

            //如果处理数据，那么处理为;
            RxBusCalendarTimeForAgreement rxBusCalendarTimeForAgreement = new RxBusCalendarTimeForAgreement();
            rxBusCalendarTimeForAgreement.setActionFlag(ExtraValue.MODIFY_ARCHIVEFILE_AGREEMENT_TIME_SELECT);
            rxBusCalendarTimeForAgreement.setAgreenMentFileAddSubmitCurrent(agreenMentFileAddSubmit);
            rxBusCalendarTimeForAgreement.setManufactureDate(calendar.getYear() + "-" + String.format("%02d", calendar.getMonth()) + "-" + String.format("%02d", calendar.getDay()));

            RxBus.getInstance().post(rxBusCalendarTimeForAgreement);

        } else if (extra_value == ExtraValue.MODIFY_PAINT_BOOKING_TIME_SELECT) {//是从【添加合同】的选择签约时间过来的

            //如果处理数据，那么处理为;
            RxBusCalendarTimeForAgreement rxBusCalendarTimeForAgreement = new RxBusCalendarTimeForAgreement();
            rxBusCalendarTimeForAgreement.setActionFlag(ExtraValue.MODIFY_PAINT_BOOKING_TIME_SELECT);
            rxBusCalendarTimeForAgreement.setManufactureDate(calendar.getYear() + "-" + String.format("%02d", calendar.getMonth()) + "-" + String.format("%02d", calendar.getDay()));

            RxBus.getInstance().post(rxBusCalendarTimeForAgreement);

        } else if (extra_value == ExtraValue.MODIFY_SIGNATURE_AGREEMENT_TIME_SELECT) {//是从新车销售的-添加签约时间过来的

            //如果处理数据，那么处理为;
            RxBusCalendarTimeForSignatureAdd rxBusCalendarTimeForSignatureAdd = new RxBusCalendarTimeForSignatureAdd();
            rxBusCalendarTimeForSignatureAdd.setActionFlag(ExtraValue.MODIFY_SIGNATURE_AGREEMENT_TIME_SELECT);
            rxBusCalendarTimeForSignatureAdd.setManufactureDate(calendar.getYear() + "-" + String.format("%02d", calendar.getMonth()) + "-" + String.format("%02d", calendar.getDay()));

            RxBus.getInstance().post(rxBusCalendarTimeForSignatureAdd);

        }


        //选定了日期之后 关闭弹窗；
        if (isClick) {
            dismiss();
        }

    }

    @Override
    public void onYearChange(int year) {
        if (mTextMonthDay != null) {
            mTextMonthDay.setText(String.valueOf(year));
        }
    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
//        {
//            ViewParent p = getParent();
//            if (p != null)
//                p.requestDisallowInterceptTouchEvent(true);
//        }
//
//        return false;
//    }
}
